// pages/songDetail/songDetail.js人、
import moment from 'moment'
import  request from '../../utils/request'
import PubSub from 'pubsub-js'
const appData = getApp()
Page({
  

  /**
   * 页面的初始数据
   */
  data: {
    isPlay:false,
    song:{},
    musicId:'',
    music:'',
    musicTime:'00:00',
    currentTime:'00:00',
    currentWidth:0
    

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (o) {
  let musicId  = o.musicId
    this.setData({
      musicId
    })
    if(appData.globalData.isMusicPlay && appData.globalData.musicId === musicId) {
      this.setData({
        isPlay:true
      })
    }
    this.getMusicInfo(o.musicId)
    this.backgroundAudioManager = wx.getBackgroundAudioManager()
    this.backgroundAudioManager.onPlay(()=> {
    this.changePlayState(true)
    appData.globalData.isMusicPlay= true
    appData.globalData.musicId= musicId
    })
    this.backgroundAudioManager.onPause(()=> {
      this.changePlayState(false)
      appData.globalData.isMusicPlay= false
    
  })
  this.backgroundAudioManager.onStop(()=> {
    this.changePlayState(false)
    appData.globalData.isMusicPlay= false
  
})
    this.backgroundAudioManager.onEnded(()=> {
      PubSub.publish('switchType','next')
      this.setData({
        currentWidth:0,
        currentTime:'00:00',
      })

    })

  this.backgroundAudioManager.onTimeUpdate(()=>{
 
    let currentTime = moment(this.backgroundAudioManager.currentTime * 1000).format('mm:ss')
    let currentWidth = this.backgroundAudioManager.currentTime/this.backgroundAudioManager.duration* 450
    this.setData({
      currentTime,
      currentWidth
    })

  })

  },
  switchMusic(e){
    let type = e.currentTarget.id;
    this.backgroundAudioManager.stop()
    // dingyue
    PubSub.subscribe('musicId',(msg,musicId) => {
      console.log(musicId);
      this.getMusicInfo(musicId)
      this.musicControl(true,musicId)
      PubSub.unsubscribe('musicId')
    })
   PubSub.publish('switchType',type)

  },
  changePlayState(isPlay){
    this.setData({
      isPlay
    })

  },
  async getMusicInfo(musicId){
  
 let songData = await request('/song/detail',{ids:musicId})
 let song =songData.songs[0]
 let musicTime = moment(song.dt).format('mm:ss')
    this.setData({
      song,
      musicTime
    })

    wx.setNavigationBarTitle({
      title: this.data.song.name,
    })
  },

  handleMusicPlay(){
    this.setData({
      isPlay:!this.data.isPlay
    })
    let {musicId,music} = this.data
    this.musicControl(this.data.isPlay,musicId,music)
  },
async  musicControl(isPlay,musicId,music){
  let musicManager = wx.getBackgroundAudioManager();
   // 创建控制音乐播放的实例
   if(!music) {
   
    
    let music = await request('/song/url',{id:musicId})
         
    this.setData({
      music: music.data[0].url
    })
   }
  
   // 获取音乐对象
  
    if(isPlay) {
      // 控制音乐
      musicManager.src =this.data. music
      musicManager.title = this.data.song.name

    }else {
      musicManager.pause()
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})